/**
 * Created by dukes on 2015/12/22.
 */
//module game{
    class TotalRechargePanel extends eui.Component{
        public constructor(){
            super();
            this.skinName = "skins.scene.TotalRechargePanelSkin";
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
        }
        private flag:boolean;
        private curConfig:RechargeActivityConfig;
        private listProvider;
        public updateDisplay(){
            this.curConfig = ActivityMgr.getInstance().getRechargeConfigByIndex(this.hehe_tab.selectedIndex+1);

            var arr:any[] = [];
            if( this.curConfig.award_catHair){
                arr.push({catHair:this.curConfig.award_catHair});
            }
            if( this.curConfig.award_diamond){
                arr.push({diamond:this.curConfig.award_diamond});
            }
            if( this.curConfig.award_dogHair){
                arr.push({dogHair:this.curConfig.award_dogHair});
            }
            if( this.curConfig.award_props){
                for(var key in this.curConfig.award_props){
                    var goodInfo:GoodInfo = Model.GetGoodInfoById(key);
                    if(goodInfo.type == Model.GOOD_TYPE_ITEM){
                        if( Model.GOOD_TYPE_CAT == Model.me.myType && goodInfo.use4Cat){
                            arr.push({item:key,count:this.curConfig.award_props[key]})
                        }else if( Model.GOOD_TYPE_DOG == Model.me.myType && goodInfo.use4Dog){
                            arr.push({item:key,count:this.curConfig.award_props[key]})
                        }
                    }else{
                        arr.push({item:key,count:this.curConfig.award_props[key]})
                    }
                }
            }
            this.listProvider.source = arr;

            var needDiamond = this.curConfig.diamond;
            var currentDiamon = ActivityMgr.getInstance().chargeInfo.total;
            this.flag = currentDiamon >= needDiamond;
            if( this.flag){
                this.group_pro.text = needDiamond+"p"+needDiamond;
                //GetUIAssetsNum(needDiamond+"/"+needDiamond,this.group_pro,1.0,true,false,null,"preload_json.num_1_");
                this.img_pro.width = 190;
                this.lab_remind.textFlow = [
                    {
                        text:"再充值",
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                    {
                        text:"0",
                        style:{
                            "textColor": 0xff0040,
                            "size":16
                        }
                    },
                    {
                        text:"钻石，可获得以下奖励",
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                ];
                if( ActivityMgr.getInstance().checkRechargeActivityState(this.curConfig.type)){
                    this.img_get.source = RES.getRes("img_complete");
                    this.img_get.touchEnabled = false;
                }else{
                    this.img_get.source = RES.getRes("img_get");
                    this.img_get.touchEnabled = true;
                }

            }else{
                this.group_pro.text = currentDiamon+"p"+needDiamond;
                //GetUIAssetsNum(currentDiamon+"/"+needDiamond,this.group_pro,1.0,true,false,null,"preload_json.num_1_");
                this.img_pro.width = 190*currentDiamon/needDiamond;
                this.lab_remind.textFlow = [
                    {
                        text:"再充值",
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                    {
                        text:""+(needDiamond-currentDiamon),
                        style:{
                            "textColor": 0xff0040,
                            "size":16
                        }
                    },
                    {
                        text:"钻石，可获得以下奖励",
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                ];
                this.img_get.source = RES.getRes("fr_recharge_btn");
                this.img_get.touchEnabled = true;
            }
        }
        private onClose(evt:egret.TouchEvent){
            if( evt )
                SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            DisplayController.instance().removeFromAlert(GameConst.GrayMask);
            DisplayController.instance().removeFromAlert(this);
        }

        private onGet(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            if( this.flag){
                window["__chargeAward"](this.curConfig.type);
            }else{
                this.onClose(null);
                //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
                BuyDiamondPanel.getInstance().show();
            }
        }

        private onSelectedTab(evt:egret.Event){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            this.updateDisplay();
        }

        private onAdded(evt:egret.Event){
            var config:RechargeActivityConfig = ActivityMgr.getInstance().getCurRechargeConfig();
            if( config ){
                if( config.type == 1){
                    this.hehe_tab.selectedIndex = 0;
                }else{
                    this.hehe_tab.selectedIndex = config.type - 2;
                }
            }else{
                this.hehe_tab.selectedIndex = this.listProvider.source.length -1;
            }
            this.updateDisplay();
            this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
            this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
            this.hehe_tab.addEventListener(egret.Event.CHANGE, this.onSelectedTab, this);
        }

        private onRemoved(evt:egret.Event){
            this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
            this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
            this.hehe_tab.removeEventListener(egret.Event.CHANGE, this.onSelectedTab, this);
        }

        public show(){
            DisplayController.instance().addToAlert(GameConst.GrayMask);
            DisplayController.instance().addToAlert(this);
        }

        public img_close:eui.Image;
        public hehe_tab:eui.TabBar;
        public img_pro:eui.Image;
        public group_pro:eui.BitmapLabel;
        public lab_remind:eui.Label;
        public list:eui.List;
        public img_get:eui.Image;
        public partAdded(name:string, instance:any){
            super.partAdded(name, instance);
            if( name == "img_close"){
                this.img_close = instance;
            }else if( name == "lab_remind"){
                this.lab_remind = instance;
            }else if( name == "hehe_tab"){
                this.hehe_tab = instance;
                this.hehe_tab.itemRenderer = TotalRechargeTabButton;
                var rechargeConfig:ActivityInfo = ActivityMgr.getInstance().getActivityConfigById(ActivityMgr.ACTIVITY_RECHARGE);
                var arr = [];
                arr[rechargeConfig.config.length-2] = 1;
                this.hehe_tab.dataProvider = new eui.ArrayCollection(arr);
                this.hehe_tab.selectedIndex = 0;
            }else if( name == "group_pro"){
                this.group_pro = instance;
                this.group_pro.textAlign = "center";
            }else if( name == "list"){
                this.list = instance;
                this.list.itemRenderer = TotalRechargeItemRender;
                this.listProvider = new eui.ArrayCollection();
                this.list.dataProvider = this.listProvider;
            }else if( name == "img_get"){
                this.img_get = instance;
            }else if( name == "img_pro"){
                this.img_pro = instance;
            }
        }
        public measure() {
            super.measure();
            this.x = GameConst.StageW - this.measuredWidth >> 1;
            this.y = GameConst.StageH - this.measuredHeight >> 1;
        }
        //public childrenCreated(){
        //    super.childrenCreated();
        //    if( !this.flag){
        //        this.updateDisplay();
        //        this.flag = true;
        //    }
        //}
        private static _instance:TotalRechargePanel;
        public static getInstance():TotalRechargePanel{
            if( TotalRechargePanel._instance == null)
                TotalRechargePanel._instance = new TotalRechargePanel();
            return TotalRechargePanel._instance;
        }
    }
//}